<template>
  <BasicModal v-bind="$attrs" destroyOnClose title="评估充值" width="40%" :okButtonProps="{ disabled: false }" @register="registerModal" @ok="handleSubmit">
    <BasicForm @register="registerForm" style="margin-top: 10px"/>
  </BasicModal>
</template>
<script lang="ts" setup>
import {BasicModal, useModalInner} from '@apps/antd-admin/src/components/modal';
import {BasicForm, useForm} from '@apps/antd-admin/src/components/form';
import {changeBusNumFormSchema} from '../data/data';
import {ref} from 'vue';
import {updateBusNum, getUserInfo} from '../data/api';

const userId = ref();
const emit = defineEmits(['success', 'register']);
const [registerForm, {resetFields, setFieldsValue, validate}] = useForm({
  labelWidth: 90,
  schemas: changeBusNumFormSchema,
  showActionButtonGroup: false,
});
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  await resetFields();
  userId.value = data.record.userId;
  let queryData = await getUserInfo({userId: userId.value});
  await setFieldsValue({
    ...queryData,
  });
});

async function handleSubmit() {
  try {
    const values = await validate();
    values.userId = userId.value;
    setModalProps({confirmLoading: true});
    await updateBusNum(values);
    closeModal();
    emit('success', false);
  } finally {
    setModalProps({confirmLoading: false});
  }
}
</script>
